package com.fyt.service.Impl.user.manager;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fyt.controller.utils.Code;
import com.fyt.controller.utils.Result;
import com.fyt.entity.User;
import com.fyt.mapper.UserMapper;
import com.fyt.service.user.manager.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.yaml.snakeyaml.events.Event;

import java.util.Date;
import java.util.List;
import java.util.Map;
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper,User> implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private PasswordEncoder passwordEncoder;

    /**
     * 用户列表
     * @param map
     * @return
     */
    @Override
    public Result getList(Map<String, String> map) {
        //获取前端需要的第几页，默认为1
        Integer page = map.get("pageNumber")==null?1:Integer.parseInt(map.get("pageNumber"));
        Integer pageSize =Integer.parseInt(map.get("pageSize")) ;
//        System.out.println("pageNumber  ：" +page);
        Page<User> userIPage = new Page<>(page, pageSize);
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(map.get("username")!=null,User::getUsername,map.get("username"))
//                .or()
                .like(map.get("accPhone")!=null,User::getAccPhone,map.get("accPhone"))
//                .or()
                .like(map.get("enable")!=null,User::getEnable,map.get("enable"));
        Page<User> userPage = userMapper.selectPage(userIPage, queryWrapper);
        System.out.println(userPage.getRecords());
        return new Result(userPage,userPage!=null?Code.get_ok:Code.get_err,userPage!=null?"success":"error");
    }

    /**
     * 新增用户
     * @param map
     * @return
     */
    @Override
    public Result addUser(Map<String,String>map) {
        String username = map.get("username");
        String phone = map.get("accPhone");
        String enable = map.get("enable");
        String password = phone.substring(5,11);
        QueryWrapper<User> queryWrapper1 = new QueryWrapper<>();
        //获取最新添加的用户的信息
        queryWrapper1.orderByDesc("create_time").last("limit 1");
        User users1 = userMapper.selectOne(queryWrapper1);
        //最新添加的用户的id一定是数据库中最大的id，然后根据id生成account
        long count = (long)users1.getId();
        count++;
        String account = "YZ"+count;
        System.out.println(count);
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("acc_phone",phone);
        User user1 = userMapper.selectOne(queryWrapper);
        if(user1!=null){
            return new Result(null,Code.save_err,"账号已存在");
        }
        Date date = new Date();
        if(username==null||phone==null|username.length()>10||phone.length()!=11){
            return new Result(null,Code.save_err,"信息有误，请检查后继续");
        }
        User user = new User();
        String encodedPassword = passwordEncoder.encode(password);
        user.setPassword(encodedPassword);
        user.setUsername(username);
        user.setAccount(account);
        user.setEnable(enable);
        user.setAccPhone(phone);
        user.setCreateTime(date);
        user.setUpdateTime(date);
        int insert = userMapper.insert(user);
        return new Result(null,insert>0?Code.save_ok:Code.save_err,insert>0?"success":"error");
    }

    /**
     * 修改用户
     * @param map
     * @return
     */
    @Override
    public Result updateUser(Map<String,String>map) {
        System.out.println(map);
        String id = map.get("id");
        String enable = map.get("enable");
        String username = map.get("username");
        String phone = map.get("accPhone");
        String account  = map.get("account");
        User user = new User();
        user.setUsername(username);
        user.setAccount(account);
        user.setAccPhone(phone);
        user.setEnable(enable);
        user.setId(Integer.parseInt(id));
        int i = userMapper.updateById(user);
        System.out.println(i);
        return new Result(null,i>0?Code.update_ok:Code.update_err,i>0?"success":"error");
    }

    @Override
    public Result resetPassword(String id) {
        User user = new User();
        user.setPassword("123456");
        user.setId(Integer.parseInt(id));
        int i = userMapper.updateById(user);
        return new Result(null,i>0?Code.update_ok:Code.update_err,i>0?"success":"error");
    }
}
